package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SCommonClassMapper;
import com.yuelife.service.pojo.SCommonClass;
import com.yuelife.service.service.SCommonClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SCommonClass业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SCommonClassServiceImpl implements SCommonClassService {

    @Autowired
    private SCommonClassMapper sCommonClassMapper;


    /**
     * SCommonClass条件+分页查询
     * @param sCommonClass 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SCommonClass> findPage(SCommonClass sCommonClass, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sCommonClass);
        //执行搜索
        return new PageInfo<SCommonClass>(sCommonClassMapper.selectByExample(example));
    }

    /**
     * SCommonClass分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SCommonClass> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SCommonClass>(sCommonClassMapper.selectAll());
    }

    /**
     * SCommonClass条件查询
     * @param sCommonClass
     * @return
     */
    @Override
    public List<SCommonClass> findList(SCommonClass sCommonClass){
        //构建查询条件
        Example example = createExample(sCommonClass);
        //根据构建的条件查询数据
        return sCommonClassMapper.selectByExample(example);
    }


    /**
     * SCommonClass构建查询对象
     * @param sCommonClass
     * @return
     */
    public Example createExample(SCommonClass sCommonClass){
        Example example=new Example(SCommonClass.class);
        Example.Criteria criteria = example.createCriteria();
        if(sCommonClass!=null){
            // 主键(自增长)评论分类id
            if(!StringUtils.isEmpty(sCommonClass.getCommonClassId())){
                    criteria.andEqualTo("commonClassId",sCommonClass.getCommonClassId());
            }
            // 评论分类名称
            if(!StringUtils.isEmpty(sCommonClass.getClassmessage())){
                    criteria.andEqualTo("classmessage",sCommonClass.getClassmessage());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sCommonClassMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SCommonClass
     * @param sCommonClass
     */
    @Override
    public void update(SCommonClass sCommonClass){
        sCommonClassMapper.updateByPrimaryKey(sCommonClass);
    }

    /**
     * 增加SCommonClass
     * @param sCommonClass
     */
    @Override
    public void add(SCommonClass sCommonClass){
        sCommonClassMapper.insert(sCommonClass);
    }

    /**
     * 根据ID查询SCommonClass
     * @param id
     * @return
     */
    @Override
    public SCommonClass findById(Integer id){
        return  sCommonClassMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SCommonClass全部数据
     *
     * @return
     */
    @Override
    public List<SCommonClass> findAll() {
        return sCommonClassMapper.selectAll();
    }
}
